package com.luozy.search.mq;

import com.luozy.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//ES与数据库 item数据同步 监听
@Component
public class ItemListener {
    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "add.queue"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = "add"
    ))
    public void listenItemAdd(Long id) {
        searchService.addItemById(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delete.queue"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = "delete"
    ))
    public void listenItemDelete(Long id) {
        searchService.deleteItemById(id);
    }
}
